# encoding: utf-8
""" 
@version: v1.0 
@author: autumner 
@license: Apache Licence  
@contact: 18322313385@163.com 
@site:  https://gitee.com/autumner/pythoncookbook
@software: PyCharm 
@file: net_urllib_http_headers.py 
@time: 2019/7/10 上午11:22
"""
'''
修改http请求头内容
'''
from urllib import request, parse

# 设置URL
url = 'http://httpbin.org/post'

# 设置http头
headers = {
    'User-agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36',
    'Spam' : 'Eggs',
"Accept-Encoding" : 'null'
}

# 设置参数
params = {
    'username' : 'zhangsan',
    'address' : 'China.Tianj'
}

# 参数转码
querstring = parse.urlencode(params)
# 发送请求
req = request.Request(url, querstring.encode('ascii'), headers=headers)

# 返回请求结果
u = request.urlopen(req)
resp = u.read()
print(resp)
